package br.com.candiolli.apoio.server;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class RestWS {

	protected static final String JNDINAME = "jdbc/ApoioJndi";
	
	private Connection connection;

	private Connection getConnection() {
		try {
			if(connection == null){
				InitialContext contexto = new InitialContext();
				DataSource ds = (DataSource) contexto.lookup("java:comp/env/"+ JNDINAME);
				connection = ds.getConnection();
			} 
			return connection;
		} catch (SQLException e) {
			return null;
		} catch (NamingException e) {
			return null;
		}
	}
	
	public ResultSet executeQuery(String query) throws SQLException{
		Statement statement = this.getConnection().createStatement();
		return statement.executeQuery(query);
	}


}
